import _ from "lodash"
import store from "store"

export function initDefaultSku(product) {
	const sku = {}
	const skus = product.skus
	for (let i = 0; i < skus.length; i++) {
		if (skus[i].stock > 0) {
			return skus[i]
		}
	}
	return sku;
}

export function initAttributeKeysCount(product) {
	return product.attribute_group.attribute_keys.length
}

export function initOriginalAttrValueIds(product) {
	let originalValueIds = []
	const skus = product.skus
	for (let i = 0; i < skus.length; i++) {
		let sku = skus[i]
		if (sku.stock > 0) {
			originalValueIds = originalValueIds.concat(sku.attr_value_index_array)
		}
	}
	return _.uniq(originalValueIds)
}

function findAvailableAttrValueIdsWhenChoose(skuAttrValueIds, currentValueId) {
	if (_.includes(skuAttrValueIds, currentValueId.toString())) {
		return skuAttrValueIds
	}
	return []
}

function findAvailableAttrValueIdsWhenCancel(skuAttrValueIds, currentValueId) {
	let selectedValueIds = store.getters.selectedAttributeValues
	_.pull(selectedValueIds, currentValueId)

	if (selectedValueIds.length > 0) {
		const intersection = _.intersection(selectedValueIds, skuAttrValueIds.map(Number))
		if (intersection.length === selectedValueIds.length) {
			return skuAttrValueIds
		}
	} else {
		return skuAttrValueIds
	}

	return []
}

function getAvailableAttrValueIds(currentValueId, isCancel = false) {
	let skus = store.getters.skus
	let availableValueIds = []
	for (let i = 0; i < skus.length; i++) {
		let sku = skus[i]

		// 1. only check sku which has stock.
		if (sku.stock > 0) {
			let skuAttrValueIdsArr = sku.attr_value_index_array
			// 2. choose a value id.
			if (currentValueId) {
				// 3. cancel id
				if (isCancel) {
					availableValueIds = availableValueIds.concat(
						findAvailableAttrValueIdsWhenCancel(skuAttrValueIdsArr, currentValueId)
					)
				}
				// 3. choose id
				else {
					availableValueIds = availableValueIds.concat(
						findAvailableAttrValueIdsWhenChoose(skuAttrValueIdsArr, currentValueId)
					)
				}
			}
			// 2. not choose any id.
			else {
				availableValueIds = availableValueIds.concat(skuAttrValueIdsArr)
			}
		}
	}
	return _.uniq(availableValueIds)
}

export function refreshAttribute(currentKeyId, currentValueId, isCancel = false) {
	let result = {}
	let attributes = store.getters.attributes
	let selectedValueIds = store.getters.selectedAttributeValues
	let availableAttrValueIds = getAvailableAttrValueIds(currentValueId, isCancel)

	_.forEach(attributes, (key) => {
		_.forEach(key.attribute_values, (value) => {

			let thisValueId = value.id
			const isThisValueUnselectable = !_.includes(availableAttrValueIds, thisValueId.toString())
			value.is_unselectable = isThisValueUnselectable

			// choose.
			if (currentValueId && !isCancel) {
				if (isThisValueUnselectable) {
					// remove from selected ids.
					_.pull(selectedValueIds, thisValueId)
				}
				if ((key.id === currentKeyId) && (thisValueId !== currentValueId)) {
					// remove from selected ids.
					_.pull(selectedValueIds, thisValueId)

					// keep available sibling value ids.
					const originalValueIds = store.getters.originalAttrValueIds
					if (_.includes(originalValueIds, thisValueId.toString())) {
						value.is_unselectable = false
					}
				}
			}

			// cancel
			if (currentValueId && isCancel) {
				_.pull(selectedValueIds, currentValueId)
			}

		})
	})

	if (currentValueId && !isCancel) {
		selectedValueIds.push(currentValueId)
	}

	result.selectedValueIds = selectedValueIds
	result.attributes = attributes
	return result
}

export function matchSelectedFinalSku() {
	let attributeKeysCount = store.getters.attributeKeysCount
	let selectedValueIds = store.getters.selectedAttributeValues
	if (selectedValueIds.length === attributeKeysCount) {
		let skus = store.getters.skus
		for (let i = 0; i < skus.length; i++) {
			let sku = skus[i]
			let skuAttrValuesArr = sku.attr_value_index_array
			if (sku.stock > 0) {
				skuAttrValuesArr = skuAttrValuesArr.map(Number) // convert fields to int
				const intersection = _.intersection(selectedValueIds, skuAttrValuesArr)
				if (intersection.length === selectedValueIds.length) {
					return sku
				}
			}
		}
		return null
	}
}
